/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application.externalServices;

/**
 *
 * @author aa6345 (based on example from http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html)
 */
import java.net.*;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/*
import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
 *
 */
/*
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
*/
public class DiscoWordnetService {

    private String fullURL;
    //private String fullText;
    private String fullResponse;
    private String searchKeyword;
    private String accessKey;

    private ArrayList<String> extractedKeywords = new ArrayList();    

    public DiscoWordnetService(String thisText, String accessKey) {
        /*thisURL has to be URL Encoded*/        
        this.setSearchKeyword(thisText);
        this.setAccessKey(accessKey);
        this.createURLWithText();
        //this.extractedKeywords = null;
    }

    public DiscoWordnetService() {
        //this.fullText = "";
        /*
        this.fullURL = "";
        this.fullResponse = "";
        this.searchKeyword = "";
        this.accessKey = "";
        */
        //this.extractedKeywords = null;
        this.setSearchKeyword("");
        this.setAccessKey("");        
    }

    public void startDiscoWordnetService() {
        try {
            //System.out.println("DW url is: "+this.getFullURL());
            URL dwservice = new URL(this.getFullURL());
            URLConnection yc = dwservice.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    yc.getInputStream()));

            String inputLine;
            String fullTmpResponse = "";
            String[] tmpt;

            while ((inputLine = in.readLine()) != null) {
                //System.out.println(inputLine + "\n");
                if (!inputLine.contains("<!--")) {
                    tmpt = inputLine.split("<!--");
                    fullTmpResponse = fullTmpResponse + tmpt[0];
                } else {
                    fullTmpResponse = fullTmpResponse + inputLine;
                }
            }
            this.setFullXMLResponse(fullTmpResponse);
            //System.out.println("DW raw response is: "+fullTmpResponse);
            //System.out.println("DW raw response is: "+this.getFullXMLResponse());
            in.close();
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }

    public void createURLWithText() {
        
        String part1 = "http://project-brain.org:8080/DiscoWordnetBasic/basicapi.htm?submit=submit&"
                       +"key="+this.getAccessKey()+"&inputWord=";

        String encodedFullText = "";
        try {            
            encodedFullText = URLEncoder.encode(this.getSearchKeyword(), "UTF-8");
        } catch (Exception e) {
            //e.printStackTrace();
        }
        this.setFullURL(part1 + encodedFullText);
        //System.out.println(this.getFullURL());
    }

    public String getFullURL() {
        return fullURL;
    }

    public void setFullURL(String fullURL) {
        this.fullURL = fullURL;
    }

    public String getFullXMLResponse() {
        return fullResponse;
    }

    public void setFullXMLResponse(String fullResponse) {
        this.fullResponse = fullResponse;
    }

    public void addToExtractedKeywords(String s) {
        this.extractedKeywords.add(s);
    }

    public ArrayList getExtractedKeywords() {
        return this.extractedKeywords;
    }

    public void setExtractedKeywords(ArrayList extractedKeywords) {
        this.extractedKeywords = extractedKeywords;
    }

    public void setSearchKeyword(String kwrd) {
        this.searchKeyword = kwrd;
    }
    
    public String getSearchKeyword() {
        return this.searchKeyword;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public void extractXMLKeywords() {
        try {
            DocumentBuilderFactory factory =
                    DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(this.getFullXMLResponse()));
            Document doc = db.parse(inStream);

            doc.getDocumentElement().normalize();

            //NodeList nodeLst = doc.getElementsByTagName("ResultSet");
            NodeList nodeLst = doc.getElementsByTagName("synonyms");

            ArrayList<String> tmpArrayList = new ArrayList<String>();
            
            //initialise this.extractedKeywords
            this.setExtractedKeywords(tmpArrayList);

            for (int s = 0; s < nodeLst.getLength(); s++) {
                Node fstNode = nodeLst.item(s);
                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element fstElmnt = (Element) fstNode;
                    NodeList resultsElmntLst = fstElmnt.getElementsByTagName("synonym");

                    for (int r = 0; r < resultsElmntLst.getLength(); r++) {
                        if (!tmpArrayList.contains(resultsElmntLst.item(r).getTextContent().trim())) {
                            tmpArrayList.add(resultsElmntLst.item(r).getTextContent().trim());
                        }
                    }

                }
            }

            this.getExtractedKeywords().addAll(tmpArrayList);

        } catch (Exception e) {
            //System.out.println(e);
        }
    }

    
}
